package com.lt.integration.qywx.service;


import com.lt.integration.qywx.constant.QyUrlConstant;
import com.lt.integration.qywx.util.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

/**
 * @ author     :  Dragon
 * @ Created in :  2022年04月26日 10:28
 */
@Slf4j
public class QyService {

    /**
     * 获取suite_access_token
     * @param suiteId        应用ID
     * @param suiteSecret    应用秘钥
     * @param suiteTicket   企业微信服务器会定时（每十分钟）推送ticket。ticket会实时变更，并用于后续接口的调用。
     * @return
     */
    public static String getSuiteAccessToken(String suiteId, String suiteSecret, String suiteTicket){
        //拼装获取access_token的url请求
        String tokenUrl = QyUrlConstant.WX_SUITE_TOKEN_URL;
        JSONObject obj = new JSONObject();
        obj.element("suite_id", suiteId);
        obj.element("suite_secret", suiteSecret);
        obj.element("suite_ticket", suiteTicket);
        JSONObject jsonObject = AuthUtil.doPostJson(tokenUrl, obj.toString());
        String access_token = jsonObject.get("suite_access_token").toString();
        log.info("【获取suite_access_token】,suite_access_token  = {}", access_token);
        return access_token;
    }

    /**
     * 获取企业 token
     * @return
     */
//    public String getCorpToken(WxCorpConfigDTO corpConfigDTO) {
//        try {
//            String corp_token_url = QyUrlConstant.GET_CORP_TOKEN_URL.replace("SUITE_ACCESS_TOKEN", getSuiteAccessToken());
//            JSONObject object = new JSONObject();
//            object.element("auth_corpid", corpConfigDTO.getCorpId());
//            object.element("permanent_code", corpConfigDTO.getPermanentCode());
//            JSONObject jsonObject = AuthUtil.doPostJson(corp_token_url, object.toString());
//            String access_token = jsonObject.get("access_token").toString();
//            log.info("获取企业 token : {}", access_token);
//            return access_token;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//
//    }
}
